#include<iostream>
using namespace std;
#include<fstream>
#include<vector>

//不要傲慢 不怕孤独
class Solution
{
public:
    void setPostOrder(const vector<int>& preOrder, const vector<int>& inOrder, vector<int>& postOrder, 
                        int prei, int prej, int ini, int inj, int posti, int postj) {
        if(prei > prej) {
            return;
        }

        if(prei == prej) {
            postOrder[posti] = preOrder[prei];
            // cout << "== : " << preOrder[prei] << endl;
            return;
        }

        if(prei < prej) {
            //postOrder[prei] 是二叉树的根节点
            int root = preOrder[prei];
            int rooti;
            // cout << "root:" << root << endl;
            //在中序序列找到root
            for(int i = ini; i <= inj; i++) {
                if(inOrder[i] == root) {
                    rooti = i;
                    break;
                }
            }
            // cout << "rooti:" << rooti << endl;
            //计算左右子树节点数目
            int LeftNodeNum = rooti - ini;
            int RightNodeNum = inj - rooti;


            // cout << "leftNodeNum:" << LeftNodeNum << endl;
            // cout << "rightNodeNum:" << rightNodeNum << endl;


            //在后序遍历中填入root
            postOrder[postj] = root;
            //递归计算左子树 右子树
            //左树
            int preli = prei + 1;
            int prelj = preli + LeftNodeNum - 1;
            int inli = ini;
            int inlj = inli + LeftNodeNum - 1;
            int postli = posti;
            int postlj = postli + LeftNodeNum - 1;
            // cout << preli << " " << prelj << endl;
            // cout << inli << " " << inlj << endl;
            // cout << postli << " " << postlj << endl;
            setPostOrder(preOrder, inOrder, postOrder, preli, prelj, inli, inlj, postli, postlj);
            //右树
            int preri = prelj + 1;
            int prerj = preri + RightNodeNum - 1;
            int inri = inlj + 2;
            int inrj = inri + RightNodeNum - 1;
            int postri = postlj + 1;
            int postrj = postri + RightNodeNum - 1;
            // cout << preri << " " << prerj << endl;
            // cout << inri << " " << inrj << endl;
            // cout << postri << " " << postrj << endl;
            setPostOrder(preOrder, inOrder, postOrder, preri, prerj, inri, inrj, postri, postrj);
        }
    }
};

ifstream in("in.txt");
int main() {
    // vector<int> prev, inv, postv;
    // int num;
    // in >> num;
    // postv.resize(num+1);
    // cout << "num:" << num << endl;
    // for(int i = 0; i < num; i++) {
    //     int tem;
    //     in >> tem;
    //     prev.push_back(tem);
    //     cout << prev[i] << " ";
    // }
    // cout << endl;
    // for(int i = 0; i < num; i++) {
    //     int tem;
    //     in >> tem;
    //     inv.push_back(tem);
    //     cout << inv[i] << " ";
    // }
    // cout << endl;
    // Solution().setPostOrder(prev, inv, postv, 0, num-1, 0, num-1, 0, num-1);
    // for(int i = 0; i < num; i++) {
    //     cout << postv[i] << " ";
    // }
    // cout << endl;



    vector<int> prev, inv, postv;
    int num;
    cin >> num;
    postv.resize(num+1);
    cout << "num:" << num << endl;
    for(int i = 0; i < num; i++) {
        int tem;
        cin >> tem;
        prev.push_back(tem);
    }
    for(int i = 0; i < num; i++) {
        int tem;
        cin >> tem;
        inv.push_back(tem);
    }
    Solution().setPostOrder(prev, inv, postv, 0, num-1, 0, num-1, 0, num-1);
    for(int i = 0; i < num; i++) {
        cout << postv[i] << " ";
    }
    cout << endl;
    return 0;
}